//
//  ActivityVC.m
//  echo
//
//  Created by dllo on 16/2/24.
//  Copyright © 2016年 Danny. All rights reserved.
//

#define WIDTH [UIScreen mainScreen].bounds.size.width / 414
#define HEIGHT [UIScreen mainScreen].bounds.size.height / 736

#import "ActivityVC.h"
#import "CellForActivityVC.h"
#import "NetworkingHandler.h"
#import "ModelForActivityCell.h"
#import <DKNightVersion.h>
#import "VCForActivityCell.h"

@interface ActivityVC ()<UITableViewDataSource, UITableViewDelegate, UITabBarControllerDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *arrForCell;
@end

@implementation ActivityVC

- (void)dealloc{
    [_arrForCell release];
    self.tableView.delegate = nil;
    [_tableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.hidesBackButton = YES;
    [self handleDataForActivityCell];
    self.tabBarController.delegate = self;

}

- (void)handleDataForActivityCell{
    [NetworkingHandler networkingGetWithUrl:@"http://echosystem.kibey.com/event/list?limit=10&type=0&page=1" dicForValue:@{@"x-v":@"20160209", @"x-c":@"3"} completeHandle:^(id result) {
        NSArray *arr = [result objectForKey:@"result"];
        self.arrForCell = [NSMutableArray array];
        for (NSDictionary *dic in arr) {
            ModelForActivityCell *model = [[ModelForActivityCell alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arrForCell addObject:model];
            [model release];
        }
        [self createTableView];
    }];

}

- (void)createTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 113) style:UITableViewStylePlain];
    self.tableView.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    
    [self.tableView registerClass:[CellForActivityVC class] forCellReuseIdentifier:@"poolForActivityCell"];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.arrForCell.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CellForActivityVC *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForActivityCell"];
    
    ModelForActivityCell *model = [self.arrForCell objectAtIndex:indexPath.row];
    [cell passDataForActivityCell:model];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    return self.view.frame.size.height / 2 - 50 * HEIGHT;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ModelForActivityCell *model = [self.arrForCell objectAtIndex:indexPath.row];
    VCForActivityCell *vc = [[VCForActivityCell alloc]init];
    vc.id = model.id;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.dk_barTintColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
        
        [DKNightVersionManager nightFalling];
        [self.navigationController.navigationBar setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor],NSFontAttributeName : [UIFont fontWithName:@"Arial" size:17]}];
    } else {
        
        [DKNightVersionManager dawnComing];
        [self.navigationController.navigationBar setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blackColor],NSFontAttributeName : [UIFont fontWithName:@"Arial" size:17]}];
    }
    self.tabBarController.tabBar.dk_barTintColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:18 / 256.f green:29 / 256.f blue:50 / 256.f alpha:1]);
    [self.tableView reloadData];

}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    if (tabBarController.selectedIndex == 1) {
        if(viewController == [tabBarController.viewControllers objectAtIndex:1])
        {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"jumpForSecond" object:nil userInfo:nil];
            return YES;
        }
    }
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
